package cn.bocweb.spinning.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.GetCommentDetailBean;
import cn.bocweb.spinning.model.Bean.InsertCommentBean;
import cn.bocweb.spinning.model.Bean.OldFangCommentsBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.InfoAllCommentAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import de.greenrobot.event.EventBus;

public class InfoAllCommentActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.all_comment_list)
    ListView allCommentList;
    @Bind(R.id.all_comment_refresh)
    SwipeRefreshLayout allCommentRefresh;

    List<GetCommentDetailBean.DataEntity> list;
    boolean isOldFang;
    InfoAllCommentAdapter adapter;
    UserPresenter presenter;
    String infoId;
    SwipeRefreshHelper swipeRefreshHelper;
    int page = 1, commentCount;
    boolean isLastPage = false;
    @Bind(R.id.all_comment_edit)
    EditText allCommentEdit;
    @Bind(R.id.all_comment_send)
    TextView allCommentSend;
    String userId;
    String icon;
    String nickname;
    InputMethodManager inputMethodManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_all_comment);


        EventBus.getDefault().register(this);
        ButterKnife.bind(this);

        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        initList();
        initToolbar();
        initIntent();
        initClickBtn();
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        if (userId == null || userId.isEmpty()) {
            UserBean.DataEntity dataEntity = new UserBean.DataEntity();
            dataEntity.readInfoFromLocal(this);
            userId = dataEntity.getUserid();
        }
    }

    private void initClickBtn() {
        allCommentSend.setOnClickListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIntent() {
        Intent intent = getIntent();
        infoId = intent.getStringExtra(Constant.INFO_DETAIL_COMMEND);
        isOldFang = intent.getBooleanExtra(Constant.INFO_DETAIL_TO_ALL_COMMENT, false);

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userId = dataEntity.getUserid();
        icon = dataEntity.getUthumb();
        nickname = dataEntity.getNickname();

        showLoading();
        presenter = new UserPresenterImp();

        if (isOldFang) {
            presenter.oldFangComments(infoId, page);
        } else {
            presenter.getCommentDetail(infoId, String.valueOf(page), null);
        }
    }

    private void initToolbar() {
        toolbar.setTitle("全部评论");
        toolbar.setNavigationIcon(R.mipmap.info_detail_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(Constant.ALL_COMMENT_DATA, commentCount);
                setResult(Constant.ALL_COMMENT_INFO_DETAIL, intent);
                finish();
            }
        });
    }

    private void initList() {
        list = new ArrayList<>();
        adapter = new InfoAllCommentAdapter(this, list);

        allCommentList.setAdapter(adapter);

        swipeRefreshHelper = new SwipeRefreshHelper(allCommentRefresh, allCommentList, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                list.clear();
                isLastPage = false;
                adapter.notifyDataSetChanged();
                allCommentRefresh.setRefreshing(false);
                if (isOldFang) {
                    presenter.oldFangComments(infoId, page);
                } else {
                    presenter.getCommentDetail(infoId, String.valueOf(page), null);
                }
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page++;
                    presenter.getCommentDetail(infoId, String.valueOf(page), null);
                } else {
                    DeviceUtil.MyToast(InfoAllCommentActivity.this, "已经是最后一页");
                }
            }
        });
    }

    public void onEventMainThread(OldFangCommentsBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            for (OldFangCommentsBean.DataEntity d: bean.getData()) {
                GetCommentDetailBean.DataEntity getCommentDetail = new GetCommentDetailBean.DataEntity();
                getCommentDetail.setUthumb(d.getUthumb());
                getCommentDetail.setAudit(d.getAudit());
                getCommentDetail.setContent(d.getContent());
                getCommentDetail.setNickname(d.getNickname());
                getCommentDetail.setTime(d.getTime());
                list.add(getCommentDetail);
            }
            adapter.notifyDataSetChanged();
        } else {
            if (bean.getReturn_code() == -203) {
                isLastPage = true;
                DeviceUtil.MyToast(this, "已经是最后一页");
            } else {
                DeviceUtil.MyToast(this, bean.getMsg());
            }
        }
    }

    public void onEventMainThread(GetCommentDetailBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();
        } else {
            DeviceUtil.MyToast(this, bean.getMsg());
            if (bean.getReturn_code() == -203) {
                isLastPage = true;
            }
        }

    }

    public void onEventMainThread(ErrorBean bean) {
        DeviceUtil.MyToast(this, bean.getErrorMsg());
        hideLoading();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.all_comment_send: {
                if (userId == null || userId.isEmpty()) {
                    intent = new Intent(this, LoginAcivity.class);
                    intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.INFODETAIL_TO_LOGIN);
                    startActivity(intent);
                } else {
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    presenter.insertComment(infoId, userId, allCommentEdit.getText().toString());
                }
                break;
            }
        }
    }

    //发送评论
    public void onEventMainThread(InsertCommentBean bean) {
        DeviceUtil.MyToast(this, bean.getMsg());
        if (bean.getReturn_code() == 0) {

            GetCommentDetailBean.DataEntity bean1 = new GetCommentDetailBean.DataEntity();
            bean1.setUthumb(icon);
            bean1.setTime("现在");
            bean1.setContent(allCommentEdit.getText().toString());
            bean1.setNickname(nickname);
            list.add(0, bean1);
            adapter.notifyDataSetChanged();
            allCommentEdit.setText("");
            commentCount++;
        }
    }
}
